//////////////////////////////
//Application focus handler class.
//////////////////////////////
package org.bbdesign.airbuzz {
	import flash.display.*;
	import flash.events.*;	
	import flash.utils.*;
	import flash.desktop.*;
	
	public class Focushandler extends Sprite  {
		//////////////////////////////	
		//Internal class variables.
		//////////////////////////////	
		//Instance.	
		private var _timer:Timer;
		
		//////////////////////////////
		//Constructor.
		//////////////////////////////		
		public function Focushandler() {			
			NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleAppActivate);
			NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleAppDeactivate);
		}
		//////////////////////////////		
		
		//////////////////////////////
		
		//////////////////////////////
		//Private, internal functions.
		//////////////////////////////
	
		//////////////////////////////
		
		//////////////////////////////
		//Application focus handling.
		//////////////////////////////
		private function handleAppActivate(e:Event):void {
			//Set timer to enable mouse events after short delay on app activation.
			_timer = new Timer(500);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, enableAppEvents);
			_timer.start();
			//Framerate throttling.
			stage.frameRate = 30;
		}
				   
		private function handleAppDeactivate(e:Event):void {
			this.mouseChildren = false;
			this.mouseEnabled = false;
			//Framerate throttling.
			stage.frameRate = 1;		
		}

		private function enableAppEvents(e:TimerEvent):void {
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, enableAppEvents);
			//Now scrolling and mouse events work.
			this.mouseChildren = true;
			this.mouseEnabled = true;
		}	
		//////////////////////////////
	}
}